<?php
namespace common\services;

use Yii;
use common\base\BaseActiveRecord;
use common\helpers\TStringHelper;
use common\models\ExhibitionHall;
use common\models\ExhibitionHallPicture;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;

class ExhibitionHallPictureServices extends ExhibitionHallPicture
{

    /**
     * 展馆图片
     * @param $params
     * @return array
     */
    public function search($params){
        $model = ExhibitionHallPicture::find(false);
        $model->orderBy('id desc');

        $dataProvider = new ActiveDataProvider([
            'query' => $model,
            'pagination' => [
                'pagesize' => $params['defaultPageSize'],
            ]
        ]);

        if (isset($params['hall_id']) && $params['hall_id'] != '') {
            $model->andFilterWhere(['=', 'hall_id', $params['hall_id']]);
        }
        if (isset($params['type']) && $params['type'] != '') {
            $model->andFilterWhere(['=', 'type', $params['type']]);
        }
        if (isset($params['status']) && $params['status'] != '') {
            $model->andFilterWhere(['=', 'status', $params['status']]);
        }

        if (!empty($params['keywords'])){
            $model->andFilterWhere(['like', 'name', $params['keywords']]);
        }

        //$sql = $model->createCommand()->getRawSql();

        return $dataProvider;
    }

    /**
     * 获取展馆图片
     * @param $hall_id
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getExhibitionHallPictureByHallId($hall_id, $type = null) {
        $model = ExhibitionHallPicture::find(false);
        $model->andFilterWhere(['=', 'status', ExhibitionHallPicture::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['=', 'hall_id', $hall_id]);
        if (!is_null($type)) {
            $model->andFilterWhere(['=', 'type', $type]);
        }
            $model->select([
                'filename' => 'name',
                'fileUrl' => 'filepath'
            ])
            ->asArray()
            ->orderBy('id asc');

        return $model->all();
    }

    /**
     * 添加展馆图片
     * @param $hall_id
     * @param $data
     * @return bool
     */
    public function setExhibitionHallPicture($hall_id, $data) {
        if (empty($hall_id) || empty($data)) return false;
        //ExhibitionHallPicture::updateAll(['status' => ExhibitionHallPicture::STATUS_FLAG_STOP],'hall_id=:hall_id and type=:type', [':hall_id' => $hall_id, ':type' => ExhibitionHallPicture::HALL_PICTURE_TYPE_PHOTO]);
        //物理删除
        Yii::$app->db->createCommand()->delete(ExhibitionHallPicture::tableName(), 'hall_id=:hall_id and type=:type', [':hall_id' => $hall_id, ':type' => ExhibitionHallPicture::HALL_PICTURE_TYPE_PHOTO])->execute();
        if (empty($data)) return false;
        if (!is_array($data)) {
            $data = json_decode($data, true);
        }

        $batchInsert = [];
        $root = \Yii::$app->basePath.'/..';
        $data = array_filter($data);
        foreach ($data as $val) {
            $json = @json_decode($val, true);
            if (!is_array($json)) continue;
            $model = new ExhibitionHallPicture();
            $model->hall_id = $hall_id;
            $model->type = ExhibitionHallPicture::HALL_PICTURE_TYPE_PHOTO;
            $model->name = $json['filename'];
            $model->filepath = $json['fileUrl'];
            $model->md5 = hash_file('md5', $root.$json['fileUrl']);
            $temp = explode('.', $json['fileUrl']);
            $model->extension = end($temp);
            array_push($batchInsert, $model);
        }
        //批量添加数据
        if (!empty($batchInsert) ) {
            BaseActiveRecord::batchInsertSqlArray($batchInsert);
        }
        return true;

    }
    /**
     * 添加展馆视频
     * @param $hall_id
     * @param $data
     * @return bool
     */
    public function setExhibitionHallVideo($hall_id, $data, $data_name) {
        if (empty($hall_id) || empty($data)) return false;
//        ExhibitionHallPicture::updateAll(['status' => ExhibitionHallPicture::STATUS_FLAG_STOP],'hall_id=:hall_id and type=:type', [':hall_id' => $hall_id, ':type' => ExhibitionHallPicture::HALL_PICTURE_TYPE_VIDEO]);
        //物理删除
        Yii::$app->db->createCommand()->delete(ExhibitionHallPicture::tableName(), 'hall_id=:hall_id and type=:type', [':hall_id' => $hall_id, ':type' => ExhibitionHallPicture::HALL_PICTURE_TYPE_VIDEO])->execute();
        if (empty($data)) return false;
        if (!is_array($data)) {
            $data = json_decode($data, true);
        }

        $batchInsert = [];
        $root = \Yii::$app->basePath.'/..';
        $data = array_filter($data);
        foreach ($data as $key => $val) {
            //$json = @json_decode($val, true);
            //if (!is_array($json)) continue;
            $model = new ExhibitionHallPicture();
            $model->hall_id = $hall_id;
            $model->type = ExhibitionHallPicture::HALL_PICTURE_TYPE_VIDEO;
            $model->name = !empty($data_name[$key]) ? $data_name[$key] : time();
            $model->filepath = $val;
            $file = $root.$val;
            $model->md5 = file_exists($file) ? hash_file('md5', $file) : md5($val);
            $temp = explode('.', $val);
            $model->extension = end($temp);
            array_push($batchInsert, $model);
        }
        //批量添加数据
        if (!empty($batchInsert) ) {
            BaseActiveRecord::batchInsertSqlArray($batchInsert);
        }
        return true;

    }

}
